package com.example.covid19.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.common.Dto.achDto;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.Achat;
import com.example.covid19.entity.Gstock;
import com.example.covid19.entity.User;
import com.example.covid19.mapper.AchatMapper;
import com.example.covid19.service.AchatService;
import com.example.covid19.service.GstockService;
import com.example.covid19.service.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalTime;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiZhengChun
 * @since 2022-12-03
 */
@RestController
@RequestMapping("/achat")
public class AchatController {
    private final AchatMapper achatMapper;
    private final UserService userService;
    private final AchatService achatService;
    private final GstockService gstockService;

    public AchatController(AchatMapper achatMapper, UserService userService, AchatService achatService, GstockService gstockService) {
        this.achatMapper = achatMapper;
        this.userService = userService;
        this.achatService = achatService;
        this.gstockService = gstockService;
    }


    @RequestMapping("/ach")
    public Result ach(@RequestBody achDto achDto)  {
        String uuid = UUID.randomUUID().toString();
        Achat achat = new Achat();
        achat.setQuantity(achDto.getQuantity());
        achat.setSsid(achDto.getSsid());
        achat.setLot(achDto.getLot());
        achat.setPrix(achDto.getPrix());
        achat.setFondateur(Integer.valueOf((String) StpUtil.getLoginId()));
        achat.setNumerotage(uuid);
        achat.setAvance(3);
        achat.setTame(LocalTime.now());
        Gstock gstock = gstockService.getOne(new QueryWrapper<Gstock>().eq("ssid",achDto.getSsid()));
        gstock.setStock(achDto.getQuantity()+gstock.getStock());
        gstock.setSsid(achDto.getSsid());
        boolean flag = achatService.save(achat)&&gstockService.updateById(gstock);
        if (flag){
            return Result.success(200,"成功",null);
        }
        else {
            return Result.fail(400,"失败",null);
        }
    }
    @RequestMapping("/achinfo")
    public Result achinfo()  {
       return Result.success(200,"查询成功",achatMapper.acginfo());
    }

}
